#include "data.h"


/*初始化环境*/
void FT_InitConfig(struct config *conf, bool flag){
	char file[32];
	size_t len;
	if(flag){
		strcpy(file,CONFIG_FILE_SERVER);
	}else{
		strcpy(file,CONFIG_FILE_CLIENT);
	}
	FILE *fp = fopen(file, "r");
	if(fp == NULL)
		ErrExit("fopen");
	/*设置IP地址*/
	fgets(conf->ip, SIZE_IP_STR, fp);
	len = strlen(conf->ip);
	if(conf->ip[len-1] == '\n')
		conf->ip[len-1] = '\0';
	/*设置端口号*/
	fgets(conf->port, SIZE_PORT_STR, fp);
	len = strlen(conf->port);
	if(conf->port[len-1] == '\n')
		conf->port[len-1] = '\0';
#ifdef DEBUG
	printf("[%s:%d] conf->ip=%s\n", __FUNCTION__, __LINE__,
			conf->ip);
	printf("[%s:%d] conf->port=%s\n", __FUNCTION__, __LINE__, 
			conf->port);
#endif
	fclose(fp);
}



/*客户端函数*/
//新增数据
void do_insert(int fd, struct data_transfer * dt){
	//发送客户端的操作类型
	Write(fd, dt, SIZE_DT_TYPE);
	//发送新增数据的id
	Write(fd, &dt->id, SIZE_DATA_ID);
	//发送新增数据的name
	Write(fd, dt->name, SIZE_DATA_NAME);
	//发送新增数据的sex
	Write(fd, &dt->sex, SIZE_DATA_SEX);
	//发送新增数据的SCORE
	Write(fd, &dt->score, SIZE_DATA_SCORE);
	//接收服务端响应
	Read(fd, &dt->type, SIZE_DT_TYPE);
	Read(fd, &dt->msgLength, SIZE_DT_MSGLENGTH);
	Read(fd, &dt->msg, dt->msgLength);
	dt->msg[dt->msgLength] = '\0';
	if(dt->type == TYPE_OK){
		printf("Success\n");
	}else{
		printf("Failed\n");
	}
	printf("%s", dt->msg);
}

//查询数据
void do_query(int fd, struct data_transfer *dt){
	//发送客户端的操作类型
	Write(fd, dt, SIZE_DT_TYPE);
	//接收服务端响应
	Read(fd, &dt->type, SIZE_DT_TYPE);
	Read(fd, &dt->msgLength, SIZE_DT_MSGLENGTH);
	Read(fd, &dt->msg, dt->msgLength);
	dt->msg[dt->msgLength] = '\0';
	if(dt->type == TYPE_OK){
		printf("Success\n");
	}else{
		printf("Failed\n");
	}
	printf("%s", dt->msg);

}

//更新数据
void do_update(int fd, struct data_transfer * dt){
	//发送客户端的操作类型
	Write(fd, dt, SIZE_DT_TYPE);
	//发送更新数据的id
	Write(fd, &dt->id, SIZE_DATA_ID);
	//发送更新数据的name
	Write(fd, dt->name, SIZE_DATA_NAME);
	//发送更新数据的sex
	Write(fd, &dt->sex, SIZE_DATA_SEX);
	//发送更新数据的SCORE
	Write(fd, &dt->score, SIZE_DATA_SCORE);
	//接收服务端响应
	Read(fd, &dt->type, SIZE_DT_TYPE);
	Read(fd, &dt->msgLength, SIZE_DT_MSGLENGTH);
	Read(fd, &dt->msg, dt->msgLength);
	dt->msg[dt->msgLength] = '\0';
	if(dt->type == TYPE_OK){
		printf("Success\n");
	}else{
		printf("Failed\n");
	}
	printf("%s", dt->msg);
	
}
//删除数据
void do_delete(int fd, struct data_transfer * dt){
	//发送客户端的操作类型
	Write(fd, dt, SIZE_DT_TYPE);
	//发送更新数据的id
	Write(fd, &dt->id, SIZE_DATA_ID);
	//接收服务端响应
	Read(fd, &dt->type, SIZE_DT_TYPE);
	Read(fd, &dt->msgLength, SIZE_DT_MSGLENGTH);
	Read(fd, &dt->msg, dt->msgLength);
	dt->msg[dt->msgLength] = '\0';
	if(dt->type == TYPE_OK){
		printf("Success\n");
	}else{
		printf("Failed\n");
	}
	printf("%s", dt->msg);	
}

/*服务端函数*/
//处理新增数据的客户端请求
void do_insert_handler(int fd, struct data_transfer * dt, sqlite3 *db){

	char sql[SIZE_SQL_LENGTH] = {};
	char *errmsg;

	//接收新增据的id
	Read(fd, &dt->id, SIZE_DATA_ID);
	//接收新增数据的name
	Read(fd, dt->name, SIZE_DATA_NAME);
	//接收新增数据的sex
	Read(fd, &dt->sex, SIZE_DATA_SEX);
	//接收新增数据的SCORE
	Read(fd, &dt->score, SIZE_DATA_SCORE);
	//服务端执行插表操作
	sprintf(sql, "insert into stu values(%d, '%s', '%c', %d)", dt->id, dt->name, dt->sex, dt->score);
	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){
		//发送服务端响应类型：失败
		dt->type = TYPE_FAIL;
		strcpy(dt->msg,errmsg);
		dt->msgLength = strlen(errmsg);
	} else{
		//发送服务端响应类型：成功
		dt->type = TYPE_OK;
		strcpy(dt->msg, "Insert done.\n");
		dt->msgLength = strlen(dt->msg);
	}
	Write(fd, &dt->type, SIZE_DT_TYPE);
	Write(fd, &dt->msgLength, SIZE_DT_MSGLENGTH);
	//发送服务端响应内容
	Write(fd, dt->msg, dt->msgLength);
}


int callback(void * qryRslt, int f_num, char ** f_value, char ** f_name){
	int i = 0;
	for(i = 0; i < f_num; i++)
	{
		char temp[50] = {0};
		sprintf(temp,"%s:%s\n", f_name[i], f_value[i]);
		strcat(qryRslt, temp);
	}
	strcat(qryRslt,"++++++++++++++++++++++\n");
	return 0;
}
//处理查询数据的客户端请求
void do_query_handler(int fd, struct data_transfer *dt, sqlite3 *db){
	char sql[SIZE_SQL_LENGTH] = "select * from stu;";
	char *errmsg;	
	char qryRslt[BUFSIZ] = {0};
	if(sqlite3_exec(db, sql, callback, qryRslt , &errmsg) != SQLITE_OK){
		//发送服务端响应类型：失败
		dt->type = TYPE_FAIL;
		strcpy(dt->msg,errmsg);
		dt->msgLength = strlen(errmsg);
	} else {
		//发送服务端响应类型：成功
		dt->type = TYPE_OK;
		strcpy(dt->msg, qryRslt);
		dt->msgLength = strlen(dt->msg);
	}
	Write(fd, &dt->type, SIZE_DT_TYPE);
	Write(fd, &dt->msgLength, SIZE_DT_MSGLENGTH);
	//发送服务端响应内容
	Write(fd, dt->msg, dt->msgLength);
}

//处理更新数据的客户端请求
void do_update_handler(int fd, struct data_transfer * dt, sqlite3 *db){
	char sql[SIZE_SQL_LENGTH] = {};
	char *errmsg;

	//接收更新据的id
	Read(fd, &dt->id, SIZE_DATA_ID);
	//接收更新数据的name
	Read(fd, dt->name, SIZE_DATA_NAME);
	//接收更新数据的sex
	Read(fd, &dt->sex, SIZE_DATA_SEX);
	//接收更新数据的SCORE
	Read(fd, &dt->score, SIZE_DATA_SCORE);
	//服务端执行插表操作
	sprintf(sql, "update stu set name='%s',sex = '%c', score= %d where id=%d",dt->name, dt->sex, dt->score, dt->id);
	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){
		//发送服务端响应类型：失败
		dt->type = TYPE_FAIL;
		strcpy(dt->msg,errmsg);
		dt->msgLength = strlen(errmsg);
	} else{
		//发送服务端响应类型：成功
		dt->type = TYPE_OK;
		strcpy(dt->msg, "Update done.\n");
		dt->msgLength = strlen(dt->msg);
	}
	Write(fd, &dt->type, SIZE_DT_TYPE);
	Write(fd, &dt->msgLength, SIZE_DT_MSGLENGTH);
	//发送服务端响应内容
	Write(fd, dt->msg, dt->msgLength);
	
}
//处理删除数据的客户端请求
void do_delete_handler(int fd, struct data_transfer *dt, sqlite3 *db){
	char sql[SIZE_SQL_LENGTH] = {};
	char *errmsg;

	//接收更新据的id
	Read(fd, &dt->id, SIZE_DATA_ID);
	//服务端执行插表操作
	sprintf(sql, "delete from stu where id = %d", dt->id);
	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){
		//发送服务端响应类型：失败
		dt->type = TYPE_FAIL;
		strcpy(dt->msg,errmsg);
		dt->msgLength = strlen(errmsg);
	} else{
		//发送服务端响应类型：成功
		dt->type = TYPE_OK;
		strcpy(dt->msg, "Delete done.\n");
		dt->msgLength = strlen(dt->msg);
	}
	Write(fd, &dt->type, SIZE_DT_TYPE);
	Write(fd, &dt->msgLength, SIZE_DT_MSGLENGTH);
	//发送服务端响应内容
	Write(fd, dt->msg, dt->msgLength);
	
}
